#!/bin/bash

##########################
# FunFile Torrent Search #
############# By: an0 ####

# Variably necessary variables.
COOKIE=funfile.org.cookie
TMP_FILE=browse.php.tmp
TMP_LIST=list.txt.tmp
RED="\033[1;31m"
GREEN="\033[1;32m"
COLOR_RESET="\033[0m"
BOLD=`tput bold`
NORMAL=`tput sgr0`

# Function junction.
get_search_parms() { # Gets and preps search string for use in URL.
 read -p "Enter search string: " STRING
 ESTRING=`echo "$STRING"|sed 's/\ /+/g'`
 if [[ -z "$ESTRING" ]];then
 echo "No URL encoded search string found. Fail."
 KEEP_COOKIE="yes" && cleanup && exit 1
 fi
}

select_type() {
 COUNT=1
 for i in "0Day" "Anime" "Applications" "Audio Books" "Ebook" "Games" "Miscellaneous" "Movies" "Music" "Portable" "TV"; do printf "$((COUNT++))) $i\n";done;printf "\n"
 read -p "Select a category [ALL]: " TYPE_SEL
 case $TYPE_SEL in
        1) CAT=45;;
        2) CAT=44;;
        3) CAT=22;;
        4) CAT=43;;
        5) CAT=27;;
        6) CAT=4;;
        7) CAT=40;;
        8) CAT=19;;
        9) CAT=6;;
        10) CAT=31;;
        11) CAT=7;;
        *) CAT=0;;
 esac
}

do_search() { # Performs search against FunFile search page.
 echo -e "\nPerforming search...\n"
 curl -Lskb $COOKIE -o "$TMP_FILE" "https://www.funfile.org/browse.php?search=${ESTRING}&sect=search&swin=title&cat=${CAT}&scat%5B%5D="
}

parser() { # Called on to parse search results.
 ROW=`grep -A7 "$i" "$TMP_FILE"`
 FILENAME=`echo "$ROW"|sed -n 1p|awk -F'"' '{print $6}'|awk -F'/' '{print $3}'|sed 's/\%/%%/g'`
 AGE=`echo "$ROW"|sed -n 5p|sed -e 's/<[^>]*>//g' 2> /dev/null|sed 's/\ //g'`
 SIZE=`echo "$ROW"|sed -n 7p|sed -e 's/<[^>]*>//g' 2> /dev/null`
 if [[ "$SIZE" == *GB ]];then
 TAB="\t\t"
 else
 TAB="\t"
 fi
}

process_req() { # Identifies requested torrent.
 REQ_FILE=`cat "$TMP_LIST" |sed -n ${REQUEST}p`
 REQ_FILE_DISPLAY=`cat "$TMP_LIST" |sed -n ${REQUEST}p|sed 's/\%28/(/g'|sed 's/\%29/)/g'|sed 's/\%20/_/g'`
 echo -e "\nYou've selected:\n${REQ_FILE_DISPLAY}\n\n"
 read -p "Is this correct? " SEL_VERIFY
 case $SEL_VERIFY in
    [yY] | [yY][Ee][Ss] ) echo -e "\n";;
      [nN] | [n|N][O|o] ) echo -e "${RED}Oh well, I tried.${COLOR_RESET}";KEEP_COOKIE="yes" && cleanup && exit 1;;
 esac
}

dl_torrent() {
 echo -e "Downloading file...\n"
 DOT_TORRENT=`echo "$REQ_FILE"|awk '{print $NF}'`
 LINK=`grep "$DOT_TORRENT" "$TMP_FILE" |head -1|awk -F'"' '{print $6}'`
 curl -Lskb $COOKIE -o "$DOT_TORRENT" "https://www.funfile.org/${LINK}"
 echo -e "File saved to ${GREEN}${DOT_TORRENT}${NORMAL}"
}

cleanup() { # Deletes temporary files and cookies.
 if [[ -z "$KEEP_COOKIE" ]];then
   if [[ -e "$COOKIE" ]];then
     rm $COOKIE
   fi
 fi

 if [[ -e "$TMP_FILE" ]];then
         rm $TMP_FILE
 fi

 if [[ -e "$TMP_LIST" ]];then
         rm $TMP_LIST
 fi
}

# Opshuns.
if [[ ! -z "$1" ]];then
 case $1 in
   cleanup) cleanup && exit 0;;
         *) echo "use cleanup argument to remove temp files and cookies" && exit 0;;
 esac
fi

# Login if needed.
if [[ ! -e "$COOKIE" ]];then
 echo -e "${BOLD}"
        read -p "Username: " USER
        stty -echo
        read -p "Password: " PASS;echo
        stty echo
 echo -e "${NORMAL}"
 curl -Lskc $COOKIE -d "returnto=/&username=${USER}&password=${PASS}&login=Login" -o /dev/null https://www.funfile.org/takelogin.php
 if [[ ! -e "$COOKIE" ]];then
 echo -e "${BOLD}${RED}Login failed! Please try again.${COLOR_RESET}${NORMAL}"
 KEEP_COOKIE="yes" && cleanup && exit 1
 fi
fi

# Get parameters for the torrent search.
get_search_parms && select_type && do_search

# Remove $TMP_LIST if it exists.
if [[ -e "$TMP_LIST" ]];then
 rm "$TMP_LIST"
fi

# Loop to process each search result.
COUNT=1
for i in `awk -F'"' '/\.torrent/ {print $6}' "$TMP_FILE" |awk -F'/' '{print $3}'`;do
 parser
 printf "$((COUNT++)) ${AGE} ${SIZE} ${FILENAME}\n" >> "$TMP_LIST"
done

# Now's as good a time as any to see if there were results.
if [[ ! -e "$TMP_LIST" ]];then
 echo -e "\n${RED}No results found.${COLOR_RESET}\n"
 KEEP_COOKIE="yes" && cleanup && exit 1
fi

# Print $TMP_LIST.
cat "$TMP_LIST" | sed 's/\%28/(/g'|sed 's/\%29/)/g'|sed 's/\%20/_/g' | awk '{printf "%-4s%-8s%-12s%s\n",$1,$2,$3,$4}'
printf "\n"

# Get selection from user.
read -p "Enter selection: " REQUEST
process_req

# Download selected .torrent file.
dl_torrent

# Clean up house and gtfo.
KEEP_COOKIE="yes" && cleanup && exit 0
